#include<iostream>
#include<string.h>
using std::cout;
using std::cin;
using std::endl;
class  Stu
{
public:
    Stu(const char* name, const int num,const char* gender)
        :_name(new char[strlen(name)+1]())
         ,_num(num)
         ,_gender(new char[strlen(gender)+1]())
    {
        cout<<"init constructor"<<endl;
        _total++;
        strcpy(_name,name);
        strcpy(_gender,gender);
    }

    Stu(const Stu & P)
        :_name(new char[strlen(P._name)+1]())
         ,_num(P._num)
         ,_gender(new char[strlen(P._gender)+1]())
    {
        _total++;
        strcpy(_name,P._name);
        strcpy(_gender,P._gender);
        cout<<"copy constructor"<<endl;
    }

    static void print(const Stu & p);

    Stu & operator=(const Stu & sh);
    ~Stu()
    {
        if(_name)
        {
            delete []_name;
            _name=nullptr;
        }
        if(_gender)
        {
            delete []_gender;
            _gender=nullptr;
        }
        cout<<"析构"<<endl;
    }
private:
    char * _name;
    int _num;
    char* _gender;
    static int _total;
};

int Stu::_total=0;

Stu & Stu::operator=(const Stu& sh)
{
    if(this!=&sh)
    {
        delete []_name;
        delete []_gender;
        _name=new char[strlen(sh._name)+1]();
        _gender=new char[strlen(sh._gender)+1]();
        strcpy(_name,sh._name);
        strcpy(_gender,sh._gender);
        _num=sh._num;
        cout<<"operator func"<<endl;
    }
    return *this;
}

void Stu::print(const Stu & p)const
{
    cout<<"name: "<<
        p._name<<" "
        <<"num: "<<p._num
        <<" gender: "<<p._gender;
    cout<<endl<<"total: " <<p._total<<endl;
}

int main()
{
    Stu p1("xixi",123,"female");
    Stu::print(p1);
    Stu p2=p1;
    Stu::print(p2);
    Stu p3("huhu",124,"male");
    Stu::print(p3);
    p3=p2;
    Stu::print(p3);

    return 0;
}



